package com.kaiwukj.android.ufamily.mvp.ui.page.mine.address;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.MyAddressUpdateParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyAddressResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.mine.MineContract;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

public class MyAddressPresenter extends BasePresenter<MineContract.Model, MineContract.AddressView> {

    public static final int ADD = 1;//add
    public static final int UPDATE = 2;//
    public static final int DELETE = 3;//

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public MyAddressPresenter(MineContract.Model model, MineContract.AddressView view) {
        super(model, view);
    }

    public void fetchList(int page, int pageSize) {
        mModel.fetchMyAddress(page, pageSize)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<MyAddressResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<MyAddressResult> result) {
                        mRootView.onFetchAddress(result.getList());
                    }
                });
    }

    public void addAddress(MyAddressUpdateParams params) {
        mModel.addAddress(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ADD) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {

                    }
                });
    }

    public void updateAddress(MyAddressUpdateParams params) {
        mModel.updateAddress(params)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, UPDATE) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {

                    }
                });
    }

    public void deleteAddress(int id) {
        mModel.deleteAddress(id)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, DELETE) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {

                    }
                });
    }

}
